package com.wu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wu.entity.Product;
import com.wu.entity.ProductCategory;
import com.wu.mapper.ProductCategoryMapper;
import com.wu.mapper.ProductMapper;
import com.wu.service.ProductCategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wu.service.ProductService;
import com.wu.vo.ProductCategoryVo;
import com.wu.vo.ProductVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wustudy
 * @since 2020-12-29
 */
@Service
public class ProductCategoryServiceImpl  implements ProductCategoryService {

    @Autowired
    private ProductCategoryMapper productCategoryMapper;

    //3.给商品信息赋值，就是首页每一大类，所属小类的商品，比如一级分类中的商品，或者二级分类的商品
    @Autowired
    private ProductMapper productMapper;


    @Override
    public List<ProductCategoryVo> getAllProductCategoryVo() {

        //一级分类
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("type",1);
        List<ProductCategory> levelOne = productCategoryMapper.selectList(queryWrapper);
        List<ProductCategoryVo> levelOneVo = levelOne.stream().map(e -> new ProductCategoryVo(e.getId(),e.getName())).collect(Collectors.toList());

        //2.这是为了加图片，要重新定义ProductCategoryVo，加了bannerImg和topImg
        for (int i = 0; i <levelOneVo.size() ; i++) {
            levelOneVo.get(i).setBannerImg("/images/banner"+i+".png");
            levelOneVo.get(i).setTopImg("/images/top"+i+".png");
            //3.跟上面接的3相对应，对商品信息进行赋值
            queryWrapper = new QueryWrapper();
            queryWrapper.eq("categorylevelone_id",levelOneVo.get(i).getId());
            List<Product> Productlist = productMapper.selectList(queryWrapper);
            List<ProductVO> productVOList = Productlist.stream().map(
                    e ->new ProductVO(
                            e.getId(),
                            e.getName(),
                            e.getPrice(),
                            "/images/"+e.getFileName()
                    )
            ).collect(Collectors.toList());
            levelOneVo.get(i).setProductVOList(productVOList);
        }

        //遍历讲二级分类封装进去
        for (ProductCategoryVo levelOneproductCategoryVo : levelOneVo) {
            queryWrapper = new QueryWrapper();
            //得到二级分类
            queryWrapper.eq("type",2);
            queryWrapper.eq("parent_id",levelOneproductCategoryVo.getId());
            List<ProductCategory> levelTwo = productCategoryMapper.selectList(queryWrapper);
            List<ProductCategoryVo> levelTwoVo = levelTwo.stream().map(e -> new ProductCategoryVo(e.getId(),e.getName())).collect(Collectors.toList());
            //封装到一级中
            levelOneproductCategoryVo.setChildren(levelTwoVo);
            for (ProductCategoryVo levelTwoproductCategoryTwoVo : levelTwoVo) {
                queryWrapper = new QueryWrapper();
                //得到三级分类
                queryWrapper.eq("type",3);
                queryWrapper.eq("parent_id",levelTwoproductCategoryTwoVo.getId());
                List<ProductCategory> levelThree = productCategoryMapper.selectList(queryWrapper);
                List<ProductCategoryVo> levelThreeVo = levelThree.stream().map(e -> new ProductCategoryVo(e.getId(),e.getName())).collect(Collectors.toList());
                levelTwoproductCategoryTwoVo.setChildren(levelThreeVo);
            }
        }
        return levelOneVo;
    }
}
